<template>
   
  <div>
    <el-dialog
      :title="form.id ? '规格添加' : '规格编辑'"
      :visible.sync="oppen.off"
    >
      {{ form }}

      <!-- //规格名称 -->
      <el-form :model="form">
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <!-- -------------------- -->

        <el-form-item
          v-for="(item, index) in attrs"
          label="规格值"
          :key="index"
          :label-width="formLabelWidth"
        >
          <div class="row">
            <el-input v-model="item.value"> </el-input>
            <el-button @click="addDomain" v-if="index === 0">新增规格</el-button   >
          <el-button @click="removeDomain(index)" v-else>删除</el-button>

          </div>

        </el-form-item>
        <!-- ------------------------- -->
        <el-form-item label="规格状态">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="oppen.off = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["oppen", "specsList"],
  data() {
    return {
      dialogFormVisible: false,
      form: {
        specsname: "", //颜色
        attrs: "", //选色
        status: 1, //状态
      },
      formLabelWidth: "120px",

      // -----

      attrs: [{ value: "" }],
    };
  },
  methods: {
    submit() {
      this.form.attrs = this.attrs.map((item) => item.value).join(",");

      this.$api.specsAdd(this.form).then((res) => {
        console.log(res);
        this.$message({
          message: "规格信息添加成功",
          type: "success",
        });
        this.form = {
          specsname: "", //颜色
          attrs: "", //选色
          status: 1, //状态
        };
        this.oppen.off = false;
        3;
      });

      this.$router.go(0);
    },
    addDomain() {
      this.attrs.push({ value: "" });
    },
    removeDomain(index) {
      this.attrs.splice(index, 1);
    },
  },
  created() {},
  mounted() {
    // 回填
    this.$BUS.$on("edits", (row) => {
      console.log(row); 
      delete row.id;
      this.form = JSON.parse(JSON.stringify(row));
      this.oppen.off = true;
    });
  },
  components: {},
  computed: {},
  watch: {},
};
</script>

<style lang='scss' scoped>
.row {
  display: flex;
}
</style>
